package com.web.user;

import com.bean.User;
import com.service.UserService;

import javax.servlet.ServletContext;
import javax.servlet.http.*;
import java.io.UnsupportedEncodingException;

public class LoginServlet extends HttpServlet {
    private static final long serialVersionUID =1L;
    public void doGet(HttpServletRequest request, HttpServletResponse response)throws UnsupportedEncodingException{
        doPost(request,response);
    }

    public void doPost(HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException {
        //1、获取登陆页面的用户名、密码
       // String username=new String(request.getParameter("username").getBytes("ISO-8859-1"),"utf-8");
        String username=request.getParameter("username");
        String password=request.getParameter("password");
        //2、调用service完成登录操作
        UserService service=new UserService();

        HttpSession session=request.getSession();
        try {
            User user=service.login(username,password);
            if(user==null){
                response.sendRedirect(request.getContextPath() + "lose.jsp");

            }else{
                //3、登录成功，将用户存入session
                //request.getSession().setAttribute("user",user);
                session.setAttribute("user",user);
                response.addCookie(new Cookie("jsessionid",session.getId()));
                response.sendRedirect(request.getContextPath() + "/admin/login/home.jsp");
            }
           // System.out.println(user);


        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
